#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "N-ary.h"

struct node* initialise(char n[10], struct node *root){
   printf("\nInitialised.");
   root=(struct node *)malloc(sizeof(struct node)); 
   strcpy(root->name,n);
   printf("%s\n",(root)->name);
   (root)->level=0;
   printf("%d\n",(root)->level);
   //(root)->fd=0;
   //printf("%d\n",(root)->fd);
   (root)->sibling=NULL;
   printf("%p\n",(root)->sibling);
   (root)->child=NULL;
   printf("%p\n",(root)->child);
   return root;
}

void insert(struct node *root,char pre[20],char name[20],int level){
  printf("\nI am in insert.");
   struct node *temp,*leaf;
   leaf=(struct node *)malloc(sizeof(struct node));
   temp=(struct node *)malloc(sizeof(struct node));      
   temp=root;
   
   
   strcpy(leaf->name,name);
   leaf->level=level;
   leaf->child=NULL;
   leaf->sibling=NULL;
   int i=0;
 //parent node
   while(/*(temp->child)!=NULL && */i<(level-1)){ 
     
     //if(i<level)
     temp=temp->child;
     i++;
     
   }

//sibling iteration  
   while((strcmp(temp->name,pre))!=0){
     temp=temp->sibling; 
   }

   if(temp->child==NULL)
    temp->child=leaf;    
   else{
   temp=temp->child;
	printf("\nshud print a : %s",temp->name);
   sinsert(temp,leaf);
   }
}
void sinsert(struct node *start,struct node *fresh){
   printf("I am in sinsert.");   
   struct node *temp;
  
   //temp=(struct node *)malloc(sizeof(struct node));
   
   temp=start;
   while(((temp->sibling)!=NULL) /*&& (strcmp(temp->name,fresh->name))!=0)*/){

     temp=temp->sibling;
   }
   if((strcmp(temp->name,fresh->name))!=0)
     temp->sibling=fresh;
}

void display1(struct node *root){
	struct node *t=root;
	while(t!=NULL)
	{
	
		printf("%s",t->name);
		t=t->sibling;
	      }
	}
